/*     */ package com.zimbra.cs.dav.caldav;
/*     */ 
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZComponent;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZParameter;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZProperty;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import java.util.Collection;
/*     */ import java.util.HashSet;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class Filter
/*     */ {
/*     */   protected String mName;
/*     */   protected boolean mIsNotDefinedSet;
/*     */   protected Range.TimeRange mTimeRange;
/*     */   protected HashSet<CompFilter> mComps;
/*     */   protected HashSet<PropFilter> mProps;
/*     */   protected HashSet<ParamFilter> mParams;
/*     */   protected HashSet<TextMatch> mTextMatches;
/*     */   
/*     */   public Filter(Element elem)
/*     */   {
/*  62 */     this.mProps = new HashSet();
/*  63 */     this.mComps = new HashSet();
/*  64 */     this.mParams = new HashSet();
/*  65 */     this.mTextMatches = new HashSet();
/*  66 */     this.mName = elem.attributeValue("name");
/*  67 */     parse(elem);
/*     */   }
/*     */   
/*     */   public String getName() {
/*  71 */     return this.mName;
/*     */   }
/*     */   
/*     */   protected void parse(Element elem) {
/*  75 */     for (Object o : elem.elements()) {
/*  76 */       if ((o instanceof Element)) {
/*  77 */         Element e = (Element)o;
/*  78 */         QName name = e.getQName();
/*  79 */         if ((canHaveCompFilter()) && (name.equals(DavElements.E_COMP_FILTER))) {
/*  80 */           this.mComps.add(new CompFilter(e));
/*  81 */         } else if ((canHavePropFilter()) && (name.equals(DavElements.E_PROP_FILTER))) {
/*  82 */           this.mProps.add(new PropFilter(e));
/*  83 */         } else if ((canHaveParamFilter()) && (name.equals(DavElements.E_PARAM_FILTER))) {
/*  84 */           this.mParams.add(new ParamFilter(e));
/*  85 */         } else if (name.equals(DavElements.E_TEXT_MATCH)) {
/*  86 */           this.mTextMatches.add(new TextMatch(e));
/*  87 */         } else if (name.equals(DavElements.E_TIME_RANGE)) {
/*  88 */           this.mTimeRange = new Range.TimeRange(e);
/*  89 */         } else if (name.equals(DavElements.E_IS_NOT_DEFINED)) {
/*  90 */           this.mIsNotDefinedSet = true;
/*     */         } else
/*  92 */           ZimbraLog.dav.info("unrecognized filter " + name.getNamespaceURI() + ":" + name.getName());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean match(ZCalendar.ZComponent comp) {
/*  98 */     return comp.getName().equals(this.mName);
/*     */   }
/*     */   
/*     */   public boolean mIsNotDefinedSet() {
/* 102 */     return this.mIsNotDefinedSet;
/*     */   }
/*     */   
/*     */   protected boolean runTextMatch(String text) {
/* 106 */     boolean matched = true;
/* 107 */     for (TextMatch tm : this.mTextMatches)
/* 108 */       matched &= tm.match(text);
/* 109 */     return matched;
/*     */   }
/*     */   
/*     */   public Range.TimeRange getTimeRange() {
/* 113 */     if (this.mTimeRange != null) {
/* 114 */       return this.mTimeRange;
/*     */     }
/* 116 */     for (Filter f : this.mComps) {
/* 117 */       Range.TimeRange tr = f.getTimeRange();
/* 118 */       if (tr != null)
/* 119 */         return tr;
/*     */     }
/* 121 */     for (Filter f : this.mProps) {
/* 122 */       Range.TimeRange tr = f.getTimeRange();
/* 123 */       if (tr != null) {
/* 124 */         return tr;
/*     */       }
/*     */     }
/* 127 */     return null;
/*     */   }
/*     */   
/* 130 */   protected boolean canHaveCompFilter() { return true; }
/* 131 */   protected boolean canHavePropFilter() { return true; }
/* 132 */   protected boolean canHaveParamFilter() { return true; }
/*     */   
/*     */   public static class TextMatch {
/*     */     private String mCollation;
/*     */     private String mText;
/*     */     private boolean mNegate;
/*     */     
/*     */     public TextMatch(Element elem) {
/* 140 */       this.mCollation = elem.attributeValue("collation", "i;ascii-casemap");
/* 141 */       this.mNegate = elem.attributeValue("negate-condition", "no").equals("yes");
/* 142 */       this.mText = elem.getText();
/*     */     }
/*     */     
/*     */     public boolean match(String val) {
/* 146 */       boolean ignoreCase = this.mCollation.equals("i;ascii-casemap");
/*     */       boolean ret;
/*     */       boolean ret;
/* 149 */       if (ignoreCase) {
/* 150 */         ret = val.equalsIgnoreCase(this.mText);
/*     */       } else
/* 152 */         ret = val.equals(this.mText);
/* 153 */       if (this.mNegate) {
/* 154 */         return !ret;
/*     */       }
/* 156 */       return ret;
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ParamFilter extends Filter {
/* 161 */     public ParamFilter(Element elem) { super(); }
/*     */     
/*     */     public boolean match(ZCalendar.ZProperty prop) {
/* 164 */       ZCalendar.ICalTok tok = ZCalendar.ICalTok.lookup(this.mName);
/* 165 */       if (tok == null)
/* 166 */         return false;
/* 167 */       ZCalendar.ZParameter param = prop.getParameter(tok);
/* 168 */       if (param == null)
/* 169 */         return this.mIsNotDefinedSet;
/* 170 */       return runTextMatch(param.getValue()); }
/*     */     
/* 172 */     protected boolean canHaveCompFilter() { return false; }
/* 173 */     protected boolean canHavePropFilter() { return false; }
/* 174 */     protected boolean canHaveParamFilter() { return false; }
/*     */   }
/*     */   
/*     */   public static class PropFilter extends Filter {
/*     */     public PropFilter(Element elem) {
/* 179 */       super();
/*     */     }
/*     */     
/*     */     public boolean match(ZCalendar.ZComponent comp) {
/* 183 */       ZCalendar.ICalTok tok = ZCalendar.ICalTok.lookup(this.mName);
/* 184 */       if (tok == null)
/* 185 */         return false;
/* 186 */       boolean matched = true;
/* 187 */       ZCalendar.ZProperty prop = comp.getProperty(tok);
/* 188 */       if (prop == null)
/* 189 */         return this.mIsNotDefinedSet;
/* 190 */       for (Filter.ParamFilter pf : this.mParams)
/* 191 */         matched &= pf.match(prop);
/* 192 */       matched &= runTextMatch(prop.getValue());
/* 193 */       return matched;
/*     */     }
/*     */     
/* 196 */     protected boolean canHaveCompFilter() { return false; }
/* 197 */     protected boolean canHavePropFilter() { return false; }
/*     */   }
/*     */   
/*     */   public static class CompFilter extends Filter {
/*     */     public CompFilter(Element elem) {
/* 202 */       super();
/*     */     }
/*     */     
/*     */     public boolean match(ZCalendar.ZComponent comp) {
/* 206 */       boolean matched = true;
/* 207 */       if (this.mName.equals("VCALENDAR")) {
/* 208 */         if (this.mComps.size() > 0) {
/* 209 */           matched = false;
/* 210 */           for (CompFilter cf : this.mComps)
/* 211 */             matched |= cf.match(comp);
/*     */         }
/* 213 */         return matched;
/*     */       }
/*     */       
/* 216 */       matched = super.match(comp);
/*     */       
/* 218 */       if ((matched) && (this.mComps.size() > 0)) {
/* 219 */         matched = false;
/*     */         
/* 221 */         for (CompFilter cf : this.mComps) {
/* 222 */           ZCalendar.ICalTok tok = ZCalendar.ICalTok.lookup(cf.mName);
/* 223 */           if (tok != null) {
/* 224 */             ZCalendar.ZComponent sub = comp.getComponent(tok);
/* 225 */             if (sub != null) {
/* 226 */               matched |= cf.match(sub);
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/* 231 */       if ((matched) && (this.mProps.size() > 0))
/*     */       {
/* 233 */         for (Filter.PropFilter pf : this.mProps) {
/* 234 */           matched &= pf.match(comp);
/*     */         }
/*     */       }
/* 237 */       return matched;
/*     */     }
/*     */     
/* 240 */     protected boolean canHaveParamFilter() { return false; }
/*     */     
/*     */     public Collection<Filter.PropFilter> getPropFilters() {
/* 243 */       return this.mProps;
/*     */     }
/*     */     
/*     */     public Collection<CompFilter> getCompFilters() {
/* 247 */       return this.mComps;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/caldav/Filter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */